#include "deleteperson.h"
#include "ui_deleteperson.h"
#include <QMessageBox>
#include <vector>
#include <QDebug>
#include "root.h"
#include "person.h"
#include "mainwindow.h"
#include "bigsave.h"
#include "teacher.h"
#include "student.h"
using namespace std;
extern vector<Teacher*> tdata;
extern vector<student*> sdata;
deleteperson::deleteperson(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::deleteperson)
{
    ui->setupUi(this);
}

deleteperson::~deleteperson()
{
    delete ui;
}

void deleteperson::on_pushButton_2_clicked()
{
    this->close();
    root* p=new(root);
    p->show();
}

void deleteperson::on_pushButton_clicked()
{
    QString str=ui->lineEdit->text();
    int r=0;
    if(str!=""){
        for(unsigned int i=0;i<tdata.size();i++){
            if(tdata[i]->findnumber()==str){
            r=1;
            QMessageBox:: StandardButton result=QMessageBox::information(this, "删除人员", "是否确认删除", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            if(result==QMessageBox::Yes){
                delete tdata[i];
                while(i<tdata.size()-1){
                    tdata[i]=tdata[i+1];
                    i++;
                }
                tdata.pop_back();
                bigsave();
            }
            }
        }
        for(unsigned int i=0;i<sdata.size();i++){
            if(sdata[i]->findnumber()==str){
            r=1;
            QMessageBox:: StandardButton result=QMessageBox::information(this, "删除人员", "是否确认删除", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            if(result==QMessageBox::Yes){
                delete sdata[i];
                while(i<sdata.size()-1){
                    sdata[i]=sdata[i+1];
                    i++;
                }
                sdata.pop_back();
                bigsave();
            }
            }
        }
        if(r==0){
            QMessageBox::critical(this, "删除人员", "查无此人");
        }
    }
}
